package com.itheima.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.system.Menu;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.IMenuService;
import com.itheima.bos.web.action.base.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * 处理菜单的action
 * @author yanju
 *
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class MenuAction extends BaseAction<Menu>{
	
	@Autowired
	private IMenuService menuServcieImpl;
	
	
	@Action(value="menu_list",results={@Result(name="success",type="json")})
	public String menu_list(){
		//查询菜单
		List<Menu> menus=menuServcieImpl.findAll();
		
		ActionContext.getContext().getValueStack().push(menus);
		return SUCCESS;
	}
	
	//菜单添加的action
	@Action(value="menu_save",results={@Result(name="success",type="redirect",location="/pages/system/menu.html")})
	public  String  menu_save(){
		menuServcieImpl.save(model);
		
		return SUCCESS;
	}
	
	
	/*
	 * 不同用登录动态展示不同的菜单列表
	 */
	
	@Action(value="menu_showMenu",results={@Result(name="success",type="json")})
	public String menu_showMenu(){
		//加载页面前查询用户具有的权限,在查询菜单表返回数据展示菜单
		
		//
		Subject subject = SecurityUtils.getSubject();
		//得到登录的用户
			 User user = (User)subject.getPrincipal();
			 
			List<Menu> results= menuServcieImpl.findByUser(user);
			 
			ActionContext.getContext().getValueStack().push(results);
		
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
}
